# -*- coding: utf-8 -*-
import re
import urllib
re_page = re.compile(r'&?page=\d+')


def set_page(url, number):
    base, query = urllib.splitquery(url)
    query = query or u''
    s = re_page.sub(u'', query).lstrip(u'&')
    if s:
        s += u'&'
    s = u'?' + s + 'page=%d'%number
    return s
